<?php

/*
 * This file is part of the Geek-Zoo Projects.
 *
 * @copyright (c) 2010 Geek-Zoo Projects More info http://www.geek-zoo.com
 * @license http://opensource.org/licenses/gpl-2.0.php The GNU General Public License
 * @author xuanyan <xuanyan@geek-zoo.com>
 *
 */
require_once ROOT_PATH.'/Abstract/Api.php';
//require_once ROOT_PATH.'/Api/Sina/weibooauth.php';
require_once ROOT_PATH.'/Api/Sina/saetv2.ex.class.php';



function id_format($array) {
    
    if (!is_array($array)) {
        return $array;
    }

    foreach ($array as $key => $val) {
        if (is_array($val)) {
            $array[$key] = id_format($val);
        }
        if ($key === 'id') {
            $array['id'] = sprintf('%1.0f', $val);
        }
    }

    return $array;
}

class ApiSina extends Api
{

    private $me = array();
    private $o = null;

    public function isBind()
    {
        if ($this->o) {
            return true;
        }
        if (empty($_SESSION['last_key']['oauth_token']) || empty($_SESSION['last_key']['oauth_token_secret'])) {
            return false;
        }
        $o = new WeiboClient($this->config['WB_AKEY'], $this->config['WB_SKEY'] , $_SESSION['last_key']['oauth_token'] , $_SESSION['last_key']['oauth_token_secret']);
        $m = $o->verify_credentials();

        if (empty($m['id'])) {
            return false;
        }

        $this->me = $m;
        $this->o = $o;

        return true;
    }

    public function friends_timeline($access_token, $param = array())
    {
        $o = new SaeTClientV2($this->config['WB_AKEY'], $this->config['WB_SKEY'] , $access_token);
        
        $m = $o->oauth->get('statuses/home_timeline', $param);
		$out_data = $m['statuses'];
        return id_format($out_data);
    }

    public function user_timeline($access_token, $param = array())
    {
        $o = new SaeTClientV2($this->config['WB_AKEY'], $this->config['WB_SKEY'] , $access_token);
		
        $m = $o->oauth->get('statuses/user_timeline', $param);
		$out_data = $m['statuses'];

        return id_format($out_data);
    }

    public function emotions($access_token)
    {
        $o = new SaeTClientV2($this->config['WB_AKEY'], $this->config['WB_SKEY'] , $access_token);
        return $o->emotions();
    }

    public function comments_to_me($access_token, $param = array())
    {
        $o = new SaeTClientV2($this->config['WB_AKEY'], $this->config['WB_SKEY'] , $access_token);
        $m = $o->oauth->get('comments/to_me', $param);
		$out_data = $m['comments'];
        return id_format($out_data);
    }
    
    public function followers($access_token, $param = array())
    {
		$param['uid'] = $param['user_id'];
		unset($param['user_id']);
        $o = new SaeTClientV2($this->config['WB_AKEY'], $this->config['WB_SKEY'] , $access_token);
        $m = $o->oauth->get('friendships/followers', $param);
        return id_format($m);
    }
    
    public function friends($access_token, $param = array())
    {
        $o = new SaeTClientV2($this->config['WB_AKEY'], $this->config['WB_SKEY'] , $access_token);
        $m = $o->oauth->get('friendships/friends', $param);
    
        //print_r($m);
        return id_format($m);
    }

    public function repost($access_token, $param) 
    {
        $o = new SaeTClientV2($this->config['WB_AKEY'], $this->config['WB_SKEY'] , $access_token);
        
        return $o->oauth->post('statuses/repost.json' , $param); 
    }

    public function counts($oauth_token, $oauth_token_secret, $ids){
		return '';
        $o = new WeiboOAuth($this->config['WB_AKEY'], $this->config['WB_SKEY'] , $oauth_token , $oauth_token_secret);
        $param = array(
            'ids' => implode(',', $ids)
        ); 
        $m = id_format($o->get('http://api.t.sina.com.cn/statuses/counts.json', $param));
        $t = array();
        foreach ($m as $key => $val) {
            if (isset($val['id'])) {
                $t[$val['id']] = $val;
                unset($t[$val['id']]['id']);
            }
        }
        return id_format($t);
    }

    public function getUserInfo($access_token)
    {
        $o = new SaeTClientV2($this->config['WB_AKEY'], $this->config['WB_SKEY'] , $access_token);
		$info = $o->get_uid();
        return $o->show_user_by_id($info['uid']);
    }

    //  发微薄
    public function update($oauth_token, $oauth_token_secret, $content, $pic = '')
    {
      $o = new WeiboClient($this->config['WB_AKEY'], $this->config['WB_SKEY'] , $oauth_token , $oauth_token_secret);

      if ($pic) {
        return id_format($o->upload($content, $pic));
      }

      return id_format($o->update($content));
    }
  
    public function mentions($access_token, $param = array())
    {
        $o = new SaeTClientV2($this->config['WB_AKEY'], $this->config['WB_SKEY'] , $access_token);
		$m = $o->oauth->get('statuses/mentions', $param);
		$out_data = $m['statuses'];
        return id_format($out_data);
    }

    public function getAuthorizeURL($url)
    {
        $o = new SaeTOAuthV2($this->config['WB_AKEY'], $this->config['WB_SKEY']);
        
        $url = $o->getAuthorizeURL($url);

        //$keys['url'] = $o->getAuthorizeURL($keys['oauth_token'] ,false , $url);

        return $url;
    }

    public function show($access_token, $param)
    {
        $o = new SaeTClientV2($this->config['WB_AKEY'], $this->config['WB_SKEY'] , $access_token);
        
        return $o->oauth->get('users/show', $param);
        
    }

	public function statuses_show($access_token, $id)
	{
		$o = new SaeTClientV2($this->config['WB_AKEY'], $this->config['WB_SKEY'] , $access_token);
        
        return $o->oauth->get('statuses/show/'.$id);
      
	}

    public function callback($keys)
    {
        $o = new SaeTOAuthV2($this->config['WB_AKEY'], $this->config['WB_SKEY']);
        try {
            $token = $o->getAccessToken('code', $keys);
        } catch (Exception $e) {
            return false;
        }
        $c = new SaeTClientV2($this->config['WB_AKEY'], $this->config['WB_SKEY'], $token['access_token']);
        $uid = $c->get_uid();

        // 调用出错的话进行处理
        $array = array(
            'access_token' => $token['access_token'],
            'id' =>  sprintf('%1.0f', $uid['uid'])
        );
		
        return $array;
    }

    public function home_timeline($access_token)
    {
        $c = new SaeTClientV2($this->config['WB_AKEY'], $this->config['WB_SKEY'], $access_token);
		$m = $c->home_timeline();
		$out_data = $m['statuses'];
    	return id_format($out_data);

    }

	public function comments($access_token, $param)
	{
		if (!isset($param['page'])) {
			$param['page'] = 0;
		}
		$o = new SaeTClientV2($this->config['WB_AKEY'], $this->config['WB_SKEY'] , $access_token);
		$m = $o->get_comments_by_sid($param['id'], $param['page']);
    	return id_format($m);
	}
	
    /////Tinge add    quq del
    // public function is_followed($oauth_token, $oauth_token_secret, $uid)
    // {
    //     $o = new WeiboClient($this->config['WB_AKEY'], $this->config['WB_SKEY'] , $oauth_token , $oauth_token_secret);
    //     $fol = $o->is_followed($uid);
    //     return id_format($fol);
    // }
    // 
    public function follow($access_token, $uid)
    {
        $o = new SaeTClientV2($this->config['WB_AKEY'], $this->config['WB_SKEY'] , $access_token);
        $isfl = $o->follow_by_id($uid);
        return id_format($isfl);
    }
    
    public function unfollow($access_token, $uid)
    {
        $o = new SaeTClientV2($this->config['WB_AKEY'], $this->config['WB_SKEY'] , $access_token);
        $unfl = $o->unfollow_by_id($uid);
        return id_format($unfl);
    }
    
    public function send_comment($access_token, $id, $con)
    {
        $o = new SaeTClientV2($this->config['WB_AKEY'], $this->config['WB_SKEY'] , $access_token);
		
        $m = $o->send_comment($id, $con, null);
		var_dump($m);
        exit;
    }

	public function statuses_reply($access_token, $param)
	{
		$o = new SaeTClientV2($this->config['WB_AKEY'], $this->config['WB_SKEY'] , $access_token);

        $m = $o->oauth->post('comments/reply', $param);
		return id_format($m);
	}


}

?>
